package main.KBProcess;

import java.io.Serializable;
import main.KBProcess.Element.ElementType;
import main.NER.Trie.ContextTrie.ORIENTATION;
import main.Ontology.ElementOntologySet;

/*
 Unstructured Text Entity Extraction System (UTEES)
 Programmer:	John Talburt
 Date:		September 13, 2008
 Filename:	KB_Index
 Purpose:	Collects and organizes entities and context from KB_Scanner
 */

public class DefaultFragment implements Serializable, Cloneable, Fragment {
	int sequenceNum;
	Element leftElement;
	Element rightElement;
	Element entityElement;

	@Override
	public void setSequenceNum(int sequenceNum) {
		this.sequenceNum = sequenceNum;
	}

	@Override
	public int getSequenceNum() {
		return sequenceNum;
	}

	@Override
	public String getDocumentLabel() {
		return entityElement == null ? null : entityElement.getDocumentCategory();
	}

	@Override
	public String getLanguageType() {
		return entityElement == null ? null : entityElement
				.getDocumentLanguage();
	}

	@Override
	public int getDocID() {
		return entityElement == null ? null : entityElement.getDocID();
	}

	@Override
	public String getLeftContext() {
		return leftElement == null ? "" : leftElement.getValue();
	}

	@Override
	public String getEntityLabel() {
		return entityElement == null ? null : entityElement.getLabel();
	}

	@Override
	public String getRightContext() {
		return rightElement == null ? "" : rightElement.getValue();
	}

	@Override
	public int getEntityLabelIndex() {
		return entityElement == null ? null : entityElement.getLabelIndex();
	}

	@Override
	public int getFragmentPosInDoc() {
		return entityElement == null ? null : entityElement.getSequenceInDoc();
	}

	@Override
	public String toString() {
//		return String
//				.format("FragID:%d|DocID:%d|DocType:%s|ETypeIndex:%d|LCxt:%s|eType:<%s>|RCxt:%s",
//						sequenceNum, getDocID(), getDocumentLabel(),
//						getEntityLabelIndex(), getLeftContext(),
//						getEntityLabel(), getRightContext());
        return String.format("%d",sequenceNum);
	}

	@Override
	public String toStringRow() {
		return String.format("%d %d %s %d {%s}<%s>{%s}", sequenceNum, getDocID(),
				getDocumentLabel(), getEntityLabelIndex(), getLeftContext(),
				getEntityLabel(), getRightContext());
	}

	@Override
	public int compareTo(Object o) {
		if (o.getClass() == Fragment.class)
			return compareTo(o);
		else
			return -2;
	}

	@Override
	public void setLeftElement(Element element) {
		leftElement = element;
	}

	@Override
	public void setRightElement(Element element) {
		rightElement = element;
	}

	@Override
	public void setEntityElement(Element element) {
		entityElement = element;
	}

	@Override
	public Element getLeftElement() {
		return leftElement;
	}

	@Override
	public Element getRightElement() {
		return rightElement;
	}

	@Override
	public Element getEntityElement() {
		return entityElement;
	}
    
    @Override
    public Element getContextElement(ORIENTATION orientation) {
        if(orientation == ORIENTATION.LEFT )
            return getLeftElement();
        else
            return getRightElement();
    }
    
    @Override
    public ElementOntologySet getOntologies(){	    
	    return ElementOntologySet.intersection(getLeftElement().getOntologies(), 
                getRightElement().getOntologies());
    }

    @Override
    public ElementOntologySet getPreFragmentOntologies() {
        ElementType type = leftElement == null ? null:leftElement.getPreElement().getType();
        return type == ElementType.ENTITY ? leftElement.getPreElement().getOntologies(): null;
    }

    @Override
    public ElementOntologySet getPostFragmentOntologies() {
        ElementType type = rightElement == null ? null:rightElement.getPostElement().getType();
        return type == ElementType.ENTITY ? rightElement.getPreElement().getOntologies(): null;
    }
}
